﻿using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class UserService
    {
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public static bool UserAdd(User user)
        {
            using (ExpertDBEntities db = new ExpertDBEntities())
            {
                db.User.Add(user);
                return db.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 用户查询
        /// </summary>
        /// <returns></returns>
        public static List<User> Userselect() 
        {
            using (ExpertDBEntities db = new ExpertDBEntities())
            {
                return db.User.ToList();
            }
        }
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool Userdelete(int id) 
        {
            using (ExpertDBEntities db=new ExpertDBEntities())
            {
                var shu = db.User.SingleOrDefault(u=>u.ID==id);
                if (shu!=null)
                {
                    db.User.Remove(shu);
                    return db.SaveChanges() > 0;
                }
                return false;
            }
        }
        /// <summary>
        /// 对用户进行封禁或解封
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool UpdateBanUser(int id)
        {
            using (ExpertDBEntities db = new ExpertDBEntities())
            {
                var user = db.User.FirstOrDefault(u => u.ID == id);
                if (user.Ban=="封禁")
                {
                    user.Ban = "正常";
                }
                else
                {
                    user.Ban = "封禁";
                }
                return db.SaveChanges() > 0;
            }
        }
    }
}
